export function setToken(token, remenber) {
  // 保存Token，默认：1天，记住我：14天
  let token_expire = new Date().getTime() + (remenber ? 14 : 1) * 24 * 60 * 60 * 1000;
  localStorage.setItem('ANIMIX_BLUE_TOKEN', token);
  localStorage.setItem('ANIMIX_BLUE_TOKEN_EXPIRE', token_expire);
}

export function getToken() {
  let token_expire = localStorage.getItem('ANIMIX_BLUE_TOKEN_EXPIRE');
  let time_now = new Date().getTime();
  let token = localStorage.getItem('ANIMIX_BLUE_TOKEN');
  if (token && token_expire && time_now < +token_expire) {
    return token;
  } else {
    try {
      localStorage.removeItem('ANIMIX_BLUE_TOKEN');
      localStorage.removeItem('ANIMIX_BLUE_TOKEN_EXPIRE');
    } catch (e) { }
    return;
  }
}

export function removeToken() {
  try {
    localStorage.removeItem('ANIMIX_BLUE_TOKEN');
    localStorage.removeItem('ANIMIX_BLUE_TOKEN_EXPIRE');
  } catch (e) { }
}

export function getLoginStatus() {
  let token_expire = localStorage.getItem('ANIMIX_BLUE_TOKEN_EXPIRE');
  let time_now = new Date().getTime();
  let token = localStorage.getItem('ANIMIX_BLUE_TOKEN');
  if (token && token_expire && time_now < +token_expire) {
    return true;
  }
  try {
    localStorage.removeItem('ANIMIX_BLUE_TOKEN');
    localStorage.removeItem('ANIMIX_BLUE_TOKEN_EXPIRE');
  } catch (e) { }
  return false;
}

export function setUser(user) {
  sessionStorage.setItem('ANIMIX_BLUE_USER', JSON.stringify(user));
}

export function getUser() {
  if (getLoginStatus()) {
    let user = sessionStorage.getItem('ANIMIX_BLUE_USER');
    if (user) {
      return JSON.parse(user);
    }
    return;
  }
  return;
}

export function removeUser() {
  try {
    sessionStorage.removeItem('ANIMIX_BLUE_USER');
  } catch (e) { }
}

export function getRight() {
  if (getLoginStatus()) {
    let rightString = sessionStorage.getItem('ANIMIX_BLUE_RIGHT');
    let right;
    try {
      right = JSON.parse(rightString);
    } catch (e) {
      right = rightString;
    }
    if (typeof right === 'string') {
      return [right];
    }
    return right;
  }
  return;
}

export function setRight(right) {
  const proRight = typeof right === 'string' ? [right] : right;
  return sessionStorage.setItem('ANIMIX_BLUE_RIGHT', JSON.stringify(proRight));
}

export function removeRight() {
  try {
    sessionStorage.removeItem('ANIMIX_BLUE_RIGHT');
  } catch (e) { }
}